package com.creatingrevolutions.vbukit.lib;

import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * Created by warodri on 7/23/13.
 */
public class FileUtils
{
	
	public static String getDefaultLocalPath() {
		return "" + Environment.getExternalStorageDirectory();
	}
	
	public static File getDefaultLocalPathasFile() {
		return Environment.getExternalStorageDirectory();
	}
	
	public static boolean writeFileToSDCard(String fileName, byte[] content, 
			boolean isFullPath)
	{
		String fullPath = getDefaultLocalPath() + "/" + fileName;
		
		if ( isFullPath ) {
			fullPath = fileName;
		}
		
		File myFile = null;
		
		try
		{
			myFile = new File(fullPath);
			myFile.createNewFile();
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(myFile));
			out.write(content);
			out.flush();
			out.close();
			myFile.canWrite();
			myFile.canRead();
			myFile.canExecute();
			Log.i("", "New file size: " + myFile.length());
			return true;
		}
		catch (Exception e)
		{
			Log.e("", "Error creating file: " + myFile != null ? myFile
					.getAbsolutePath() : "myFile is null.");
			
			Log.e("", "Error creating file: " + e.getLocalizedMessage());
			
			e.printStackTrace();
		}
		return false;
		
	}
	
	public static boolean writeFileToSDCard(String fileName, byte[] content)
	{
		return writeFileToSDCard(fileName, content, false);
	}
	
	public static boolean writeFileToSDCard(InputStream input, String targetPath)
	{
	    final File file = new File(targetPath);
        try 
        {
        	final OutputStream output = new FileOutputStream(file);
            final byte[] buffer = new byte[1024];
            int read;

            while ((read = input.read(buffer)) != -1)
                output.write(buffer, 0, read);

            output.flush();
            input.close();
            output.close();
            return true;
        }
        catch ( Exception e )
        {
        	e.printStackTrace();
        } 
		return false;
	}
	
	public static boolean deleteFile(String path)
	{
		boolean deleteSuccess = true;
		try
		{
			File file = new File(path);
			file.delete();
		}
		catch (Exception e)
		{
			deleteSuccess = false;
		}
		
		return deleteSuccess;
	}
	
	public static void deleteFilesInsideFolder(String folderPath)
	{
		File file = new File(folderPath);
		File[] files = file.listFiles();
		if (files != null)
		{
			for (File f : files)
			{
				f.delete();
			}
		}
	}
	
	/**
     * Concerts input stream to a String.
     * @param is InputStream
     * @return String representation of InputStream.
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is), 8 * 1024);
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    
    public static boolean unpackZip(String path, String zipname)
    {       
         InputStream is;
         ZipInputStream zis;
         try 
         {
             String filename;
             is = new FileInputStream(path + zipname);
             zis = new ZipInputStream(new BufferedInputStream(is));          
             ZipEntry ze;
             byte[] buffer = new byte[1024];
             int count;

             while ((ze = zis.getNextEntry()) != null) 
             {
                 // zapis do souboru
                 filename = ze.getName();

                 // Need to create directories if not exists, or
                 // it will generate an Exception...
                 if (ze.isDirectory()) {
                    File fmd = new File(path + filename);
                    fmd.mkdirs();
                    continue;
                 }

                 FileOutputStream fout = new FileOutputStream(path + filename);

                 // cteni zipu a zapis
                 while ((count = zis.read(buffer)) != -1) 
                 {
                     fout.write(buffer, 0, count);             
                 }

                 fout.close();               
                 zis.closeEntry();
             }

             zis.close();
         } 
         catch(IOException e)
         {
             e.printStackTrace();
             return false;
         }
         return true;
    }
    
}
